package com.sonic.simple.controller;

import com.alibaba.fastjson.JSONObject;
import com.sonic.simple.config.WebAspect;
import com.sonic.simple.models.http.RespEnum;
import com.sonic.simple.models.http.RespModel;
import com.sonic.simple.models.ResultDetail;
import com.sonic.simple.services.ResultDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZhouYiXun
 * @des
 * @date 2021/8/29 16:59
 */
@Api(tags = "测试结果详情相关")
@RestController
@RequestMapping("/api/controller/resultDetail")
public class ResultDetailController {
    @Autowired
    private ResultDetailService resultDetailService;

    @WebAspect
    @PostMapping
    public RespModel save(@RequestBody JSONObject jsonObject) {
        resultDetailService.saveByTransport(jsonObject);
        return new RespModel(RespEnum.HANDLE_OK);
    }

    @WebAspect
    @ApiOperation(value = "查找测试结果详情", notes = "查找对应测试结果详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "caseId", value = "测试用例id", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "resultId", value = "测试结果id", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "type", value = "类型", dataTypeClass = String.class),
            @ApiImplicitParam(name = "page", value = "页码", dataTypeClass = Integer.class)
    })
    @GetMapping("/list")
    public RespModel<Page<ResultDetail>> findAll(@RequestParam(name = "caseId") int caseId,
                                                 @RequestParam(name = "resultId") int resultId,
                                                 @RequestParam(name = "deviceId") int deviceId,
                                                 @RequestParam(name = "type") String type,
                                                 @RequestParam(name = "page") int page) {
        Pageable pageable = PageRequest.of(page - 1, 20);
        return new RespModel(RespEnum.SEARCH_OK,
                resultDetailService.findAll(resultId, caseId, type, deviceId, pageable));
    }

    @WebAspect
    @ApiOperation(value = "查找测试结果详情2", notes = "查找对应测试结果详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "caseId", value = "测试用例id", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "resultId", value = "测试结果id", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "type", value = "类型", dataTypeClass = String.class),
    })
    @GetMapping("/listAll")
    public RespModel<List<ResultDetail>> findAll(@RequestParam(name = "caseId") int caseId,
                                                 @RequestParam(name = "resultId") int resultId,
                                                 @RequestParam(name = "deviceId") int deviceId,
                                                 @RequestParam(name = "type") String type) {
        return new RespModel(RespEnum.SEARCH_OK,
                resultDetailService.findAll(resultId, caseId, type, deviceId));
    }
}
